package handler

import (
	"chat_user/api/request"
	"chat_user/inter/dao"
	"chat_user/inter/model"
	"chat_user/pkg"
	"github.com/gin-gonic/gin"
	"net/http"
)

func Login(c *gin.Context) {
	var req request.Login
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "验证失败",
			"data": err.Error(),
		})
		return
	}
	var user model.Users
	dao.GetOneByFields(&model.Users{}, &user)
	if user.Account != req.Account {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "此账号不存在",
			"data": nil,
		})
		return
	}

	claims := pkg.CustomClaims{ID: uint(user.Id)}
	jwt := pkg.NewJWT("2211a")
	token, err := jwt.CreateToken(claims)
	if err != nil {
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"code": http.StatusOK,
		"msg":  "ok",
		"data": token,
	})
}
